<?php

function hex2bin($str) {
    $bin = "";
    $i = 0;
    do {
        $bin .= chr(hexdec($str{$i}.$str{($i + 1)}));
        $i += 2;
    } while ($i < strlen($str));
    return $bin;
}

function parse_pdk_file($file_name)
{
    $ini_array = parse_ini_file($file_name, true);
    
    if (empty($ini_array))
    {
	return $ini_array;
    }
    
    $str_prj = "";
    
    for($i = 0; $i < 10; $i++)
    {
	$str_prj .= $ini_array['Project']['Adr'. $i];
    }
    
    $bin_prj = hex2bin($str_prj);
    
    $key = '';
    
    for($i = 0; $i < 16; $i+=2 )
    {
	
	$bin_prj{0x10c1 + $i} = chr(ord($bin_prj{0x10c1 + $i}) ^ 0x55 ^ 0xAA);
	$bin_prj{0x10c1 + $i + 1} = chr(ord($bin_prj{0x10c1 + $i + 1}) ^ 0x55 ^ 0xAA);
	
	if ($i == 0)
	    $key .= sprintf("%02x%02x", ord($bin_prj{0x10c1 + $i}), ord($bin_prj{0x10c1 + $i + 1})); 
	else    
	    $key .= sprintf("-%02x%02x", ord($bin_prj{0x10c1 + $i}), ord($bin_prj{0x10c1 + $i + 1})); 
    }
    
    $answer['c_num'] = ord($bin_prj{0x10bf});
    $answer['key'] = $key;
    $answer['program'] = base64_encode($bin_prj);
    
    return $answer;
}

?>
